var MainPage = window.NameSpace || {};

MainPage.loadBiInfoBtn = function(flag) {
	if (flag == 0) {
		// 当日
		if ($('#mp_today_count').prop("checked") == true) {
			$('#mp_total_count').prop("checked", false);
			MainPage.loadBiInfo(flag);
		}
	} else {
		// 累计
		if ($('#mp_total_count').prop("checked") == true) {
			$('#mp_today_count').prop("checked", false);
			MainPage.loadBiInfo(flag);
		}
	}

}

MainPage.loadBiInfo = function(flag) {
	if (flag == null)
		flag = 0;
	$.ajax({
				url : "/mainpage/loadBI",
				data : {
					"flag" : flag
				// 'create_time':create_time
				},
				success : function(data) {
					/*
					 * <h6>在线航班数(架) = flight_num</h6> <h6>在网航班数(架)
					 * =flight_online_num</h6> <h6>互联网用户使用数(人) num_user_ku</h6>
					 * <h6>局域网用户使用数(人) =num_user_wifi</h6> <h6>页面浏览量(次) = pv</h6>
					 * <h6>互联网流量(MB) =internet_flow</h6> <h6>应用访问人数(人) =uv</h6>
					 * <h6>应用访问次数(次) =count_access</h6> <h6>应用访问时长(小时)
					 * =time_day</h6> <h6>应用访问平均时长(小时) =time_once</h6>
					 */
					// bi信息设置
					$('#mp_flight_num').text(
							formatNum(data.bi.flight_num, 0, 1));
					$('#mp_flight_online_num').text(
							formatNum(data.bi.flight_online_num, 0, 1));
					$('#mp_num_user_ku').text(
							formatNum(data.bi.num_user_ku, 0, 1));
					$('#mp_num_user_wifi').text(
							formatNum(data.bi.num_user_wifi, 0, 1));
					$('#mp_pv').text(formatNum(data.bi.pv, 0, 1));
					// $('#mp_internet_flow').text(formatNum(data.bi.internet_flow,0,1));
					$('#mp_uv').text(formatNum(data.bi.uv, 0, 1));
					$('#mp_count_access').text(
							formatNum(data.bi.count_access, 0, 1));
					$('#mp_time_day').text(formatNum(data.bi.time_day, 0, 1));
					$('#mp_time_once').text(formatNum(data.bi.time_once, 0, 1));
					
					$('#mp_update_time').text(new Date().format('yyyy-MM-dd hh:mm:ss'));
					
					setTimeout(MainPage.loadBiInfo,mp_refresh_time);
				},
				error: function(XMLHttpRequest, textStatus, errorThrown){
					setTimeout(MainPage.loadBiInfo  , mp_refresh_time);
				}
			});
}


var mp_dataTableconfig = {
		
		"iDisplayLength":5,
		"processing" : true,
		"serverSide" : true,
		"paging" : true,
		"lengthChange" : false,
		"searching" : false,
		"ordering" : false,
		"info" : true,
		"autoWidth" : false,
		"oLanguage" : {
			"sProcessing" : "正在加载中......",
			// "sLengthMenu" : "每页显示 _MENU_ 条记录",
			"sZeroRecords" : "对不起，查询不到相关数据！",
			"sEmptyTable" : "暂无航班数据存在！",
			"sInfo" : "当前在线航班共 _TOTAL_ 条",
			"sInfoFiltered" : "数据表中共为 _MAX_ 条记录",
			"sSearch" : "搜索",
			"oPaginate" : {
				// "sFirst" : "首页",
				"sPrevious" : "<<",
				"sNext" : ">>",
				// "sLast" : "末页"
			}
		},
		// select
		// flight_id,flight_code,from_airport,to_airport,fly_time,land_time
		ajax : "/mainpage/loadFlightList",
		columns : [
				{
					"data" : "flight_code"
				}, {
					"data" : "line"
				}, {
					"data" : {"fly_time":"fly_time","land_time":"land_time"},
					render : function(data, type, full) {
						if(data != null){
							return '<lable style="color:#0596ea">'+data.fly_time.toDate().format('hh:mm tt')+'</label>'+'-'+
							'<lable style="color:#ffae00">'+data.land_time.toDate().format('hh:mm tt')+'</label>';
						}else{
							return '-';
						}
						
					}
				},
				{
					"data" : "org_name"
				},{
					data : "flight_id",
					render : function(data, type, full) {
						return '<button class="detail-btn " onclick=\"MainPage.showDetail('+data+');\">详情</button>';
					}
				}]
	};


MainPage.loadPlaneList = function() {
	$('#flightlist_table').DataTable(mp_dataTableconfig);
}

MainPage.nextPage = function(){
	if($('#flightlist_table_next').prop('class').indexOf('disabled')==-1){
		$('#flightlist_table_next').click();
	}else{
		$('#flightlist_table_previous').next().click();
	}
	setTimeout(MainPage.nextPage,mp_refresh_time);
}

MainPage.showDetail=function(flightId){
	$.ajax({
		url : "/mainpage/showDetail",
		async : false,
		data:{
			flight_id:flightId
		},
		success : function(data) {
			$("#mp_flight_code").text(data.flight_code);
			$("#mp_flight_line").text(data.from_airport+" - "+data.to_airport);
			$("#mp_fly_time").text(data.fly_time);
			$("#mp_land_time").text(data.land_time);
			var status = '';
			if (data.status == 0) {// 待起飞
				status = '<font color="#ff6666">待起飞</font>';
			} else if (data.status  == 1) {// 飞行中
				status = '<font color="#0099cc">飞行中</font>';
			} else if (data.status  == 2) {// 降落
				status = '<font color="#99cc00">降落</font>';
			}	
			$("#mp_flight_status").html(status);
			
			$("#mp_planeInfo").modal("toggle");
		},error: function(XMLHttpRequest, textStatus, errorThrown){
			console.log('exception>>'+textStatus);
		}
	});
	
}